//
//  ProduceLineKindViewController.m
//  MagictoHire
//
//  Created by 锋 on 2017/12/15.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "ProduceLineKindViewController.h"

@interface ProduceLineKindViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *produceLineKindTableView;
@property (nonatomic, strong) NSMutableArray *produceLineKindArr;
@end

@implementation ProduceLineKindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self setupProduceLineKindTableView];
    [self requestProduceLineKindData];
    // Do any additional setup after loading the view.
}

- (void)setupUI {
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"产线类型";
    self.view.backgroundColor = RGB(242, 242, 242);
}

- (void)setupProduceLineKindTableView {
    _produceLineKindTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - STA_NAV_HEIGHT - SCREER_HeiX) style:UITableViewStyleGrouped];
    _produceLineKindTableView.scrollEnabled = NO;
    _produceLineKindTableView.separatorColor = RGB(242, 242, 242);
    _produceLineKindTableView.delegate = self;
    _produceLineKindTableView.dataSource = self;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.000001)];
    view.backgroundColor = [UIColor whiteColor];
    _produceLineKindTableView.tableHeaderView = view;
    _produceLineKindTableView.tableFooterView = view;
    _produceLineKindTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_produceLineKindTableView];
}

- (void)requestProduceLineKindData {
    WEAKSELF;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [HYBaseHandler requestProduceLineKindParmers:nil Success:^(id obj)
     {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         if ([obj server_Getcodecheck])
         {
             NSLog(@"%@", obj);
             _produceLineKindArr = [ProduceLineKindModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
             if (_produceLineKindTableView) {
                 CGFloat height = _produceLineKindArr.count * 44;
                 if (height > SCREEN_HEIGHT - STA_NAV_HEIGHT - SCREER_HeiX) {
                     height = SCREEN_HEIGHT - STA_NAV_HEIGHT - SCREER_HeiX;
                     _produceLineKindTableView.scrollEnabled = YES;
                 } else {
                     _produceLineKindTableView.scrollEnabled = NO;
                 }
                 _produceLineKindTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, height);
             }
             [_produceLineKindTableView reloadData];
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
         
     }];
}

// MARK:tableview的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _produceLineKindArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.000001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.000001;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *ID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.textLabel.textColor = RGB(77, 77, 77);
    ProduceLineKindModel *model = _produceLineKindArr[indexPath.row];
    cell.textLabel.text = model.name;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WEAKSELF;
    ProduceLineKindModel *model = _produceLineKindArr[indexPath.row];
    weakSelf.produceLineKindBlock(model);
    [weakSelf.navigationController popViewControllerAnimated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
